﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SWF.Decomposition
{
    public static class TimDecomposer
    {
        public static short[] Decompose(List<SWF.Runtime.Vertex> contour)
        {
            short[] tmpIndices = new short[(contour.Count - 1) * 3];

            ////////  Create triangles (via indices) also considers counter clockwise ordering  ///////
            short current = 0, left = (short)(contour.Count - 1), right = 1;
            for (int i = 0; i < (contour.Count - 1) * 3; )
            {
                tmpIndices[i++] = current;
                tmpIndices[i++] = right;
                tmpIndices[i++] = left;

                current = right;
                right = (short)((right + 1) % (contour.Count + 1));

                if (i >= (contour.Count - 1) * 3) break;

                tmpIndices[i++] = current;
                tmpIndices[i++] = right;
                tmpIndices[i++] = left;

                current = left;
                left = (short)((left - 1) < 0 ? contour.Count : left - 1);
            }

            return tmpIndices;
        }
    }
}
